import dayjs from "dayjs"
Page({
  /**
   * 页面的初始数据
   */
  data: {
    startTime: "",
    endTime: "",
    days: 1,
    formatter: function (day: any) {
      // 获取日历每一天对应毫秒数
      if (day.type != "disabled") {
        day.type = ""
        day.bottomInfo = ""
      }
      let date = dayjs(day.date).toDate().getTime()
      //获取每一天对应的月份
      let dateMonth = dayjs(day.date).month()
      //获取每一天对应的日期
      let dateDay = dayjs(day.date).date()
      //获取用户选择的日期对应毫秒数
      let start = getApp().globData.start.toDate().getTime()
      let startMonth = getApp().globData.start.month()
      let startDay = getApp().globData.start.date()

      let end = getApp().globData.end.toDate().getTime()
      let endMonth = getApp().globData.end.month()
      let endDay = getApp().globData.end.date()
      if (dateMonth == startMonth && dateDay == startDay) {
        day.type = "start"
        day.bottomInfo = "开始"
      }
      if (date > start && date < end) {
        day.type = "middle"
        day.bottomInfo = "入住"
      }
      if (dateMonth == endMonth && dateDay == endDay) {
        day.type = "end"
        day.bottomInfo = "结束"
      }
      return day
    },
  },
  dateInit() {
    let { start, end } = getApp().globData
    let startTime = start.format("M月D日")
    let endTime = end.format("M月D日")
    let days = end.diff(start, "day")
    this.setData({
      startTime,
      endTime,
      days,
    })
  },
  onSelect(e: any) {
    this.setData({
      formatter: function (day: any) {
        // 获取日历每一天对应毫秒数
        let date = dayjs(day.date).toDate().getTime()
        //获取每一天对应的月份
        let dateMonth = dayjs(day.date).month()
        //获取每一天对应的日期
        let dateDay = dayjs(day.date).date()
        //获取用户选择的日期对应毫秒数
        let start = getApp().globData.start.toDate().getTime()
        let startMonth = getApp().globData.start.month()
        let startDay = getApp().globData.start.date()

        let end = getApp().globData.end.toDate().getTime()
        let endMonth = getApp().globData.end.month()
        let endDay = getApp().globData.end.date()
        if (dateMonth == startMonth && dateDay == startDay) {
          day.type = "start"
          day.bottomInfo = "开始"
        }
        if (date > start && date < end) {
          day.type = "middle"
          day.bottomInfo = "入住"
        }
        if (dateMonth == endMonth && dateDay == endDay) {
          day.type = "end"
          day.bottomInfo = "结束"
        }
        return day
      },
    })

    if (e.detail[1]) {
      let start = dayjs(e.detail[0])
      let end = dayjs(e.detail[1])
      getApp().globData.start = start
      getApp().globData.end = end
      let days = end.diff(start, "day")
      let startTime = start.format("M月D日")
      let endTime = end.format("M月D日")
      this.setData({
        startTime,
        endTime,
        days,
        formatter: function (day: any) {
          // 获取日历每一天对应毫秒数
          let date = dayjs(day.date).toDate().getTime()
          //获取每一天对应的月份
          let dateMonth = dayjs(day.date).month()
          //获取每一天对应的日期
          let dateDay = dayjs(day.date).date()
          //获取用户选择的日期对应毫秒数
          let start = getApp().globData.start.toDate().getTime()
          let startMonth = getApp().globData.start.month()
          let startDay = getApp().globData.start.date()

          let end = getApp().globData.end.toDate().getTime()
          let endMonth = getApp().globData.end.month()
          let endDay = getApp().globData.end.date()
          if (dateMonth == startMonth && dateDay == startDay) {
            day.type = "start"
            day.bottomInfo = "开始"
          }
          if (date > start && date < end) {
            day.type = "middle"
            day.bottomInfo = "入住"
          }
          if (dateMonth == endMonth && dateDay == endDay) {
            day.type = "end"
            day.bottomInfo = "结束"
          }
          return day
        },
      })
    }
  },
  onConfirm() {
    wx.navigateBack()
  },
  onShow() {
    this.dateInit()
  },
})
